﻿package ascript.components.physics
{
	//#########################
	// Header Includes
	//#########################
	import ascript.Game;
	import ascript.utilities.PointSimple;
	import ascript.GameComponent;
	import ascript.GameLevel;
	import ascript.GameObject;
	import Box2D.Collision.b2AABB;
	import Box2D.Common.Math.b2Vec2;
	
	//#########################
	// PhysicsPlayer Class
	//#########################
	public class PhysicsThrust extends GameComponent
	{
		//=========================
		// Constructors
		//=========================	
		public function PhysicsThrust():void
		{
		}
		
		//=========================
		// Members
		//=========================		
		override public function initComponent( tGame:Game, tObject:GameObject ):void
		{
			// Default Attributes
			defaultAttribute( tObject, "PhysicsEnabled", true );
			defaultAttribute( tObject, "ThrustEnabled", true );
			defaultAttribute( tObject, "Velocity", new b2Vec2() );
			defaultAttribute( tObject, "VelocityMax", new b2Vec2( 200, 200 ) );
			defaultAttribute( tObject, "Acceleration", new PointSimple( 100, 100 ) );
			defaultAttribute( tObject, "Direction", new b2Vec2( 0, 0 ) );
		}
		
		override public function updateComponent( tGame:Game, tObject:GameObject, tLevel:GameLevel ):void
		{
			if ( tObject.getAttribute( "ThrustEnabled" ) && tObject.getAttribute( "PhysicsEnabled" ) )
			{
				var tempVelocity:b2Vec2 = ( tObject.getAttribute( "Velocity" ) ).Copy();
				
				// Set X Velocity
				if ( ( tObject.getAttribute( "Direction" ) ).x != 0 )
				{
					tempVelocity.x += tObject.getAttribute( "Acceleration" ).x * tObject.getAttribute( "Direction" ).x;
									
					// Limit Max
					if ( Math.abs( tempVelocity.x ) > tObject.getAttribute( "VelocityMax" ).x )
					{
						tempVelocity.x = tObject.getAttribute( "VelocityMax" ).x * tObject.getAttribute( "Direction" ).x;
					}
				}
				else
				{
					tempVelocity.x = ( ( tObject.getAttribute( "Physics" ) ).GetLinearVelocity() ).x;
				}
				
				// Set Y Velocity
				if ( ( tObject.getAttribute( "Direction" ) ).y != 0 )
				{
					tempVelocity.y += tObject.getAttribute( "Acceleration" ).y * tObject.getAttribute( "Direction" ).y;
					
					// Limit Max
					if ( Math.abs( tempVelocity.y ) > tObject.getAttribute( "VelocityMax" ).y )
					{
						tempVelocity.y = tObject.getAttribute( "VelocityMax" ).y * tObject.getAttribute( "Direction" ).y;
					}
				}
				else
				{
					tempVelocity.y = ( ( tObject.getAttribute( "Physics" ) ).GetLinearVelocity() ).y;
				}
								
				// Apply Location
				( tObject.getAttribute( "Physics" ) ).SetAwake( true );
				( tObject.getAttribute( "Physics" ) ).SetLinearVelocity( tempVelocity );
				tObject.setAttribute( "Velocity", tempVelocity );
				tObject.setAttribute( "Location", new b2Vec2( ( ( ( tObject.getAttribute( "Physics" ) ).GetPosition() ).x * tLevel.getAttribute( "PhysicsDefaultPixelPerMeter" ) ), ( ( ( tObject.getAttribute( "Physics" ) ).GetPosition() ).y * tLevel.getAttribute( "PhysicsDefaultPixelPerMeter" ) ) ) );
			
				// Depth Sort
				if ( tempVelocity.y != 0 )
				{
					tLevel.sortObjectsLevel();
				}
			}
		}
	}
}